﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Ebada.Platform.Model;
using Ebada.UI.Base;
using Ebada.Client;
using Ebada.Core;
using Ebada.Client.Platform;
namespace Ebada.Modules.Vehicle
{
    /// <summary>
    /// 违章记录编辑
    /// </summary>
    public partial class frmVehicleActivityXEdit : FormBase, IPopupFormEdit {

        private string operType = "";

        public string OperType
        {
            get { return operType; }
            set { 
                operType = value;
                if (operType == "5")
                {
                    this.Text = "编辑洗车费用登记";
                    groupControlOrg.Text = "洗车费用登记";
                  
                }
                if (operType == "6")
                {
                    this.Text = "编辑过路费用登记";
                    groupControlOrg.Text = "过路费用登记";
                  
                }
                if (operType == "7")
                {
                    this.Text = "编辑蓬靠费用登记";
                    groupControlOrg.Text = "蓬靠费用登记";
                  
                }
            }
        }


        public frmVehicleActivityXEdit() {
            InitializeComponent();
        }
        void dataBind() {

            this.txtVehicleNumber.DataBindings.Add("EditValue", rowData, "VehicleNumber");
            this.txtAct_Code.DataBindings.Add("EditValue", rowData, "Act_Code");
            this.txtAct_Name.DataBindings.Add("EditValue", rowData, "Act_Name");
            this.txtAccountCode.DataBindings.Add("EditValue", rowData, "AccountCode");
            this.txtAccountTitle.DataBindings.Add("EditValue", rowData, "AccountTitle");

            this.dateStartDate.DataBindings.Add("EditValue", rowData, "StartDate");
            this.txtBOrgcnName.DataBindings.Add("EditValue", rowData, "BOrgcnName");
            this.txtPrice.DataBindings.Add("EditValue", rowData, "Price");
            this.txtNumber.DataBindings.Add("EditValue", rowData, "Number");
            this.txtTotalPrice.DataBindings.Add("EditValue", rowData, "TotalPrice");
            this.txtOfferAmount.DataBindings.Add("EditValue", rowData, "OfferAmount");
            this.txtEmployeeName.DataBindings.Add("EditValue", rowData, "EmployeeName");
            this.txtRemark.DataBindings.Add("EditValue", rowData, "Remark");
        }
        #region IPopupFormEdit Members
        private VehicleActivity rowData = null;

        public object RowData {
            get {
                return rowData;
            }
            set {
                if (value == null) return;
                if (rowData == null) {
                    this.rowData = value as VehicleActivity;
                    dataBind();
                    InitComboBoxData();
                } else {
                    ConvertHelper.CopyTo<VehicleActivity>(value as VehicleActivity, rowData);
                    InitComboBoxData();
                }
            }
        }

        #endregion

        private void InitComboBoxData() {
           
            IList<Vehicles> vlist = MainHelper.GetSqlMap<Vehicles>().GetListByWhere<Vehicles>(" order by VehicleNumber");
            txtVehicleNumber.Properties.DataSource = vlist;
            IList<Accounts> acclist = MainHelper.GetSqlMap<Accounts>().GetListByWhere<Accounts>(" where DebitOrCredit like '%行政%' order by AccountCode");
            txtAccountTitle.Properties.DataSource = acclist;
            IList<Empolyees> elist = MainHelper.GetSqlMap<Empolyees>().GetListByWhere<Empolyees>(" where 1=1 order by Empolyee_Name");
            txtEmployeeName.Properties.DataSource = elist;
            if (operType == "5")
            {
                IList<Activities> list = MainHelper.GetSqlMap<Activities>().GetListByWhere<Activities>(" where ProjectName='车辆项目' and Act_Name='洗车'");
                if (list != null && list.Count > 0)
                {
                    rowData.Act_Code = list[0].Act_Code;
                    rowData.Act_Name = list[0].Act_Name;
                }
            }
            if (operType == "6")
            {
                IList<Activities> list = MainHelper.GetSqlMap<Activities>().GetListByWhere<Activities>(" where ProjectName='车辆项目' and Act_Name='过路'");
                if (list != null && list.Count > 0)
                {
                    rowData.Act_Code = list[0].Act_Code;
                    rowData.Act_Name = list[0].Act_Name;
                }
            }
            if (operType == "7")
            {
                IList<Activities> list = MainHelper.GetSqlMap<Activities>().GetListByWhere<Activities>(" where ProjectName='车辆项目' and Act_Name='蓬靠'");
                if (list != null && list.Count > 0)
                {
                    rowData.Act_Code = list[0].Act_Code;
                    rowData.Act_Name = list[0].Act_Name;
                }
            }
            if (rowData.VehicleNumber == "")
            {
                rowData.Date3 = DateTime.Now;
                rowData.StartDate = DateTime.Now;
                rowData.ExpirationDate = DateTime.Now;
            }
        }
 
        private void btnOK_Click(object sender, EventArgs e)
        {
            
            if (txtAccountTitle.Text == "")
            {
                MessageBox.Show("科目名称不能为空。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtAccountTitle.Focus();
                return;
            }
            if (txtVehicleNumber.Text == "")
            {
                MessageBox.Show("车牌号不能为空。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtVehicleNumber.Focus();
                return;
            }
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

       
        private void txtAccountTitle_EditValueChanged(object sender, EventArgs e)
        {
            if (txtAccountTitle.Text != "")
            {
                rowData.AccountCode = ((Accounts)txtAccountTitle.Properties.GetDataSourceRowByKeyValue(txtAccountTitle.Text)).AccountCode;
                txtAccountCode.Text = rowData.AccountCode;
            }
        }

        private void txtVehicleNumber_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
          
            frmVehiclesSeletcter frm = new frmVehiclesSeletcter();
            frm.InitData_Slect("");
            if (frm.ShowDialog() == DialogResult.OK)
            {
                txtVehicleNumber.Text = frm.SelectObj.VehicleNumber;
                txtVehicleNumber.ClosePopup();
            }
        }

        private void txtEmployeeName_EditValueChanged(object sender, EventArgs e)
        {

        }

        private void txtEmployeeName_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            NameClick();
        }

        private void txtEmployeeName_Click(object sender, EventArgs e)
        {
            NameClick();
        }

        public void NameClick()
        {
            Ebada.Client.Platform.Selector.frmEmpSelectorByOrgInfo frm = new Ebada.Client.Platform.Selector.frmEmpSelectorByOrgInfo();
            frm.StartPosition = FormStartPosition.CenterScreen;
            frm.InitionData(string.Format(" where 1=1", this.txtEmployeeName.Text), false, false);
            if (frm.ShowDialog() == DialogResult.OK)
            {
                txtEmployeeName.Text = frm.CurrentEmp.Empolyee_Name;
                rowData.EmployeeID = frm.CurrentEmp.Empolyee_ID;
                txtEmployeeName.ClosePopup();
            }
        }
        public void OrgClick()
        {
            frmOrgSelector2 frm = new frmOrgSelector2();
            frm.IntionDataAll();
            if (frm.ShowDialog() == DialogResult.OK)
            {
                rowData.BOrgcnName = frm.SelectObj.cnName;
                txtBOrgcnName.Text = frm.SelectObj.cnName;
                rowData.BOrgCode = frm.SelectObj.OrgCode;
            }
        }
        private void txtBOrgcnName_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            OrgClick();
        }

        private void txtBOrgcnName_Click(object sender, EventArgs e)
        {
            OrgClick();
        }

        private void txtPrice_EditValueChanged(object sender, EventArgs e)
        {
            txtTotalPrice.Value = txtPrice.Value * txtNumber.Value;
            rowData.TotalPrice = txtPrice.Value * txtNumber.Value; ;

        }

        private void txtNumber_EditValueChanged(object sender, EventArgs e)
        {
            txtTotalPrice.Value = txtPrice.Value * txtNumber.Value;
            rowData.TotalPrice = txtPrice.Value * txtNumber.Value; ;
        }
      
    }
}